package com.ztools.pc.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztools.common.model.pc.sys.Permission;
import com.ztools.common.model.pc.sys.PermissionTreeItem;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.pc.TokenAuthorization;
import com.ztools.pc.service.sys.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/4  19:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @TokenAuthorization("/sys/permission/search")
    @RequestMapping("/list")
    public Result findList(@RequestBody PageUtil<Permission> pageUtil) {
        Page<Permission> list = permissionService.findList(pageUtil);
        return list != null ? Result.success("查询成功").data(list) : Result.error("查询失败");
    }

    @TokenAuthorization("/sys/permission/search")
    @RequestMapping("/treeData")
    public Result treeData() {
        List<PermissionTreeItem> permissionTreeData = permissionService.findPermissionTreeData();
        return Result.success("查询成功").data(permissionTreeData);
    }

    @TokenAuthorization("/sys/permission/add")
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        boolean b = permissionService.add(permission);
        return b ? Result.success("添加成功") : Result.error("添加成功");
    }

    @TokenAuthorization("/sys/permission/update")
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission) {
        boolean b = permissionService.update(permission);
        return b ? Result.success("修改成功") : Result.error("修改失败");
    }

    @TokenAuthorization("/sys/permission/delete")
    @RequestMapping("/delete")
    public Result delete(@RequestBody Permission permission) {
        boolean b = permissionService.delete(permission);
        return b ? Result.success("删除成功") : Result.error("删除失败");
    }

}
